﻿using System.Collections.Generic;
using System.Data;

namespace Common.Utility
{
    public class ParamCollection
    {

        private List<Param> _SqlParamList = new List<Param>();
        /// <summary>
        /// 参数对象内部集合
        /// </summary>
        public List<Param> GetParamList
        {
            get
            {
                return this._SqlParamList;
            }
        }
        /// <summary>
        /// 返回集合中数据长度
        /// </summary>
        public int Length
        {
            get
            {
                return this._SqlParamList.Count;
            }
        }
        /// <summary>
        /// 添加参数对象
        /// </summary>
        /// <param name="param">参数对象</param>
        public void AddParam(Param param)
        {
            this._SqlParamList.Add(param);
        }

        #region 冗余注释 添加单个参数简写
        ///// <summary>
        ///// 添加 单个 Param
        ///// </summary>
        ///// <param name="key"></param>
        ///// <param name="value"></param>
        //public void AddParam(string key, object value)
        //{
        //    this.AddParam(new Param(key, value));
        //}
        ///// <summary>
        ///// 添加 单个 Param
        ///// </summary>
        ///// <param name="key">键</param>
        ///// <param name="value">值</param>
        ///// <param name="dbType">类型枚举</param>
        //public void AddParam(string key, object value, DbType dbType)
        //{
        //    this.AddParam(new Param(key, value, dbType));
        //}

        //public void AddParam(string key, object value, DbType dbType, ParameterDirection inOutType, int length)
        //{
        //    this.AddParam(new Param(key, value, dbType, inOutType, length));
        //} 
        #endregion

        /// <summary>
        /// 添加参数数组
        /// </summary>
        /// <param name="collection">参数数组</param>
        public void AddParam(Param[] collection)
        {
            this._SqlParamList.AddRange(collection);
        }
        /// <summary>
        /// 添加参数集合ParamCollection
        /// </summary>
        /// <param name="collection">集合对象</param>
        public void AddParam(ParamCollection collection)
        {
            this._SqlParamList.AddRange(collection.ToParamArray());
        }
        /// <summary>
        /// 返回参数数组对象
        /// </summary>
        /// <returns>参数数组</returns>
        public Param[] ToParamArray()
        {
            return this._SqlParamList.ToArray();
        }
        /// <summary>
        /// 返回参数列表
        /// </summary>
        /// <returns>参数列表</returns>
        public List<Param> ToParamList()
        {
            return this._SqlParamList;
        }
        /// <summary>
        /// 删除指定参数对象
        /// </summary>
        /// <param name="param">参数对象</param>
        /// <returns>成功或失败</returns>
        public bool RemoveParam(Param param)
        {
            return this._SqlParamList.Remove(param);
        }
        /// <summary>
        /// 根据Key值删除指定参数对象
        /// </summary>
        /// <param name="keyParam">Key值</param>
        /// <returns>成功或失败</returns>
        public bool RemoveParam(string keyParam)
        {
            bool result;
            foreach (Param param in this._SqlParamList)
            {
                if (param.KeyParam == keyParam)
                {
                    result = this._SqlParamList.Remove(param);
                    return result;
                }
            }
            result = false;
            return result;
        }
        /// <summary>
        /// 清空集合
        /// </summary>
        public void Clear()
        {
            this._SqlParamList.Clear();
        }
        /// <summary>
        /// 根据keyParam值返回指定参数对象
        /// </summary>
        /// <param name="keyParam">key值</param>
        /// <returns>参数对象</returns>
        public Param GetParam(string keyParam)
        {
            Param result;
            foreach (Param param in this._SqlParamList)
            {
                if (param.KeyParam == keyParam)
                {
                    result = param;
                    return result;
                }
            }
            result = null;
            return result;
        }
        /// <summary>
        /// 根据keyParam值返回参数对应的值
        /// </summary>
        /// <param name="keyParam">paramName </param>
        /// <returns>object值</returns>
        public object GetParamValue(string keyParam)
        {
            object result;
            foreach (Param param in this._SqlParamList)
            {
                if (param.KeyParam == keyParam)
                {
                    result = param.ValueParam;
                    return result;
                }
            }
            result = null;
            return result;
        }
    }
}
